define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Groupbuydetail, $stateParams, $window, $timeout, $ionicSlideBoxDelegate,GoPage,$interval,Auth,ChatInfo,GoDialog) {
		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
		//获取数据
		$scope.act_id = $stateParams.act_id;
		$scope.act_type = $stateParams.type;
		//加载数据
		$scope.init_data = function() {
//			//获取详细数据
//			Groupbuydetail.get_detail($scope.act_id, function(data) {
//				$scope.detailData = data;
//			});
			Groupbuydetail.groupbuy_goods_detail($scope.act_id,function(data,now_time){
				if(data.success==true){
					$scope.datas = data.result;
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
				$scope.init_time(data.result,now_time);
				$scope.set_timer(now_time);
				$scope.msg = '';
				}else{
					$scope.msg = data.msg;
				}
			});
			$scope.get_loginfo();
//			Groupbuydetail.groupbuy_detail($scope.act_id,function(data){
//				$scope.datas = data;
//				
//			})
		}
		//轮播小圆点切换
		$scope.advsgo = function(index) {
			$ionicSlideBoxDelegate.$getByHandle('advs-handle').slide(index);
		}
		//滑动时候触发
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectTab = index;
		};

		$scope.isHide = true;//默认隐藏
		//隐藏弹窗
		$scope.hide_apply = function(){
			$scope.isHide = true;
		}
		//显示弹窗
		$scope.show_apply = function(value){
			$scope.isHide = false;
            $scope.word_id = value;
		}	
		$scope.formate_time = function(time) {
//			console.log(time);
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
			} else {
				return '0天0小时0分0秒';
			}
		};
		$scope.init_time = function(data, now_time){
			if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else{
					if($scope.act_type==1){
						data.start_time = data.start_time - now_time;
						$scope.act_time_name = '距开始';
					}else{
						data.now_time = data.end_time - now_time;
						$scope.act_time_name = '距结束';
					}
				}
					
				data.formate_time = $scope.formate_time(data.now_time);
		}
		$scope.set_timer = function(now_time) {
			$scope.timer =  $interval(function() {
				if($scope.datas.now_time != undefined)
					$scope.datas.now_time = $scope.datas.now_time - 1;
				else{
					if($scope.act_type==1){
						$scope.datas.now_time = $scope.datas.start_time - now_time;
					}else{
						$scope.datas.now_time = $scope.datas.end_time - now_time;
					}
				}
					
				$scope.datas.formate_time = $scope.formate_time($scope.datas.now_time);
			}, 1000)
		}
		$scope.go_exchangechat = function(group_id, is_member) {
			if(is_member!=true){
				Groupbuydetail.join_group(group_id, function(data) {
					if(data.rtnCode == 0 || data.rtnCode == 5) {
								GoPage.go_states("exchange_interaction_chat", {
									'group_id': group_id
								});
							}
					 else {
						GoDialog.toast(data.rtnMsg);
					}
				})
			}else{
				GoPage.go_states("exchange_interaction_chat", {
									'group_id': group_id
								});
			}
		}
		//小小图登录信息
		$scope.get_loginfo = function() {
			//判断本地是否保存有小小图登录信息
			var is_chatLogin = ChatInfo.get_isInfos();
			if(is_chatLogin == false && Auth.check_loginInfo())
				Groupbuydetail.get_loginfo(function(data) {
					$scope.chat_url = data.chat_url;
					$scope.login_code = data.login_code;
					$scope.soket_url = data.soket_url;
					//初次保存小小图登录信息
					ChatInfo.save_chat_loginInfo($scope.chat_url, $scope.login_code, $scope.soket_url);
				})
		};
		$scope.go_goods = function(goods_id,product_id){
//			console.log($scope.act_id);
			if(Auth.check_user_type()<=4)
			GoPage.go_states("groupbuy_goods",{'goods_id':goods_id,'act_id':$scope.act_id,'product_id':product_id});
			else{
				GoDialog.toast('您不满足参与条件！')
			}
		}
	}

	controller.$inject = ['$scope', 'Groupbuydetail', '$stateParams', '$window', '$timeout', '$ionicSlideBoxDelegate','GoPage','$interval','Auth','ChatInfo','GoDialog'];

	return app.registerController("groupbuydetailctrl",controller);
});